package com.hellpro.lolbox.home.dasheng;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.LolBaseActivity;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.home.HomeJsonUtils;
import com.hellpro.lolbox.utils.OkHttpClientUtils;
import com.hellpro.lolbox.video.bean.VideoManitoBean;
import com.hellpro.lolbox.video.model.VideoManitoLvAdapter;
import com.hellpro.lolbox.video.presenter.VideoPresenter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class DaSheng extends LolBaseActivity {
    private VideoManitoLvAdapter adapter;

    @BindView(R.id.activity_dasheng_lv)
    PullToRefreshListView mLv;
    private VideoPresenter presenter;
    private  List<VideoManitoBean> data;
    //使用Handler回调数据
    private   Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                data = (List<VideoManitoBean>) msg.obj;
                Log.i("aa", data.size() + "dddddddddddddddd");
                adapter.setData(data);
            }
        }
    };



    @Override
    public void initView() {
        adapter = new VideoManitoLvAdapter();
        mLv.setAdapter(adapter);
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        String path = UrlConfigs.ViDEO_MONITO_PATH;
                        try {
                            String stringFromURL = OkHttpClientUtils.getStringFromURL(DaSheng.this, path, null);
                            Type type = new TypeToken<List<VideoManitoBean>>() {
                            }.getType();
                            List<VideoManitoBean> list = new ArrayList<VideoManitoBean>();
                            list = (List<VideoManitoBean>) HomeJsonUtils.getArrayData(stringFromURL, type);
                            Log.i("aa", "aawwss" + list.size());

                                Message obtain = Message.obtain();
                                obtain.what = 1;
                                obtain.obj = list;
                                mHandler.sendMessage(obtain);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }
        ).start();
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_da_sheng;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}
